<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      <el-form-item label="聊天推送" prop="isChatSend">
        <el-select v-model="queryParams.isChatSend" placeholder="请选择聊天推送" clearable size="small">
          <el-option
            v-for="dict in isChatSendOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="活动推送" prop="isActSend">
        <el-select v-model="queryParams.isActSend" placeholder="请选择活动推送" clearable size="small">
          <el-option
            v-for="dict in isActSendOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="系统推送" prop="isSysSend">
        <el-select v-model="queryParams.isSysSend" placeholder="请选择系统推送" clearable size="small">
          <el-option
            v-for="dict in isSysSendOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="发布活动" prop="isPuhAct">
        <el-select v-model="queryParams.isPuhAct" placeholder="请选择允许发布活动" clearable size="small">
          <el-option
            v-for="dict in isPuhActOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="会员等级" prop="levelId">
        <el-select v-model="queryParams.levelId" placeholder="请选择会员等级" clearable size="small">
          <el-option
            v-for="dict in levelIdOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="账号" prop="userName">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入账号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="邮箱" prop="email">
        <el-input
          v-model="queryParams.email"
          placeholder="请输入邮箱"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="地址" prop="address">
        <el-input
          v-model="queryParams.address"
          placeholder="请输入地址"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号" prop="phone">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入手机号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="昵称" prop="nickName">
        <el-input
          v-model="queryParams.nickName"
          placeholder="请输入昵称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="性别" prop="sex">
        <el-select v-model="form.sex" placeholder="请选择性别">
          <el-option
            v-for="dict in sexOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['member:memberbasis:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['member:memberbasis:edit']"
        >审核</el-button>
      </el-col>

    </el-row>

    <el-table v-loading="loading" :data="memberbasisList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="账号" align="center" prop="userName" />
      <el-table-column label="头像" align="center" prop="avterImg" >
        <template slot-scope="scope">
          <el-image :src="imgpre+ scope.row.avterImg"  :preview-src-list="showImg(scope.row.avterImg)"></el-image>
        </template>
      </el-table-column>
      <el-table-column label="昵称" align="center" prop="nickName" />
      <el-table-column label="性别" align="center" prop="sex" :formatter="sexFormat"  />
      <el-table-column label="聊天推送" align="center" prop="isChatSend" :formatter="isChatSendFormat" />
      <el-table-column label="活动推送" align="center" prop="isActSend" :formatter="isActSendFormat" />
      <el-table-column label="系统推送" align="center" prop="isSysSend" :formatter="isSysSendFormat" />
      <el-table-column label="发布权限" align="center" prop="isPuhAct" :formatter="isPuhActFormat" />
      <el-table-column label="会员等级" align="center" prop="levelId" :formatter="levelIdFormat" />
      <!--<el-table-column label="密码" align="center" prop="passWord" />-->
      <!--<el-table-column label="微信标识" align="center" prop="openId" />-->
      <el-table-column label="邮箱" align="center" prop="email" />
      <el-table-column label="地址" align="center" prop="address" />
      <el-table-column label="职业" align="center" prop="career" />
      <el-table-column label="手机号" align="center" prop="phone" />
      <el-table-column label="生日" align="center" prop="brith" width="180">
      </el-table-column>
     <!-- <el-table-column label="主键" align="center" prop="id" />-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['member:memberbasis:edit']"
          >审核</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-key"
            @click="handleResetPwd(scope.row)"
            v-hasPermi="['system:user:resetPwd']"
          >重置密码</el-button>
        </template>
         <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['member:memberbasis:remove']"
          >删除</el-button>-->

      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改会员基本信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="聊天推送">
          <el-select v-model="form.isChatSend" placeholder="请选择聊天推送">
            <el-option
              v-for="dict in isChatSendOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="活动推送">
          <el-select v-model="form.isActSend" placeholder="请选择活动推送">
            <el-option
              v-for="dict in isActSendOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="系统推送">
          <el-select v-model="form.isSysSend" placeholder="请选择系统推送">
            <el-option
              v-for="dict in isSysSendOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="活动权限">
          <el-select v-model="form.isPuhAct" placeholder="请选择活动权限">
            <el-option
              v-for="dict in isPuhActOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="会员等级">
          <el-select v-model="form.levelId" placeholder="请选择会员等级">
            <el-option
              v-for="dict in levelIdOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="地址" prop="address">
          <el-input v-model="form.address" placeholder="请输入地址" />
        </el-form-item>
        <el-form-item label="职业" prop="career">
          <el-input v-model="form.career" placeholder="请输入职业" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listMemberbasis, getMemberbasis,resetUserPwd, delMemberbasis, addMemberbasis, updateMemberbasis, exportMemberbasis } from "@/api/member/memberbasis";
import defaultSettings from '@/settings'
export default {
  name: "memberbasis",
  data() {
    return {
      imgpre: defaultSettings.ossPreview,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 会员基本信息表格数据
     memberbasisList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 聊天是否推送字典
      isChatSendOptions: [],
      // 活动是否推送字典
      isActSendOptions: [],
      // 系统推送字典
      isSysSendOptions: [],
      // 是否允许发布活动字典
      isPuhActOptions: [],
      // 是否允许发布活动字典
      sexOptions: [],
      // 会员等级字典
      levelIdOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        isChatSend: undefined,
        isActSend: undefined,
        isSysSend: undefined,
        isPuhAct: undefined,
        levelId: undefined,
        userName: undefined,
        email: undefined,
        address: undefined,
        phone: undefined,
        nickName: undefined,
        sex: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        sex: [
          { required: true, message: "性别不能为空", trigger: "blur" }
        ],
        nickName: [
          { required: true, message: "昵称不能为空", trigger: "blur" }
        ],
        userName: [
          { required: true, message: "账号不能为空", trigger: "blur" }
        ],
        phone: [
          { required: true, message: "手机号不能为空", trigger: "blur" },
          {
            pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
            message: "请输入正确的手机号码",
            trigger: "blur"
          }
        ],
        email: [
          { required: true, message: "邮箱不能为空", trigger: "blur" },
          {
            type: "email",
            message: "'请输入正确的邮箱地址",
            trigger: ["blur", "change"]
          }
        ],
        isPuhAct: [
          { required: true, message: "是否允许发布活动不能为空", trigger: "blur" }
        ],
        isSysSend: [
          { required: true, message: "系统推送不能为空", trigger: "blur" }
        ],
        isActSend: [
          { required: true, message: "活动是否推送不能为空", trigger: "blur" }
        ],
        isChatSend: [
          { required: true, message: "聊天是否推送不能为空", trigger: "blur" }
        ]

      }
    };
  },
  created() {
    this.getList();
    this.getDicts("sys_yes_no").then(response => {
      this.isChatSendOptions = response.data;
    });
    this.getDicts("sys_yes_no").then(response => {
      this.isActSendOptions = response.data;
    });
    this.getDicts("sys_yes_no").then(response => {
      this.isSysSendOptions = response.data;
    });
    this.getDicts("sys_yes_no").then(response => {
      this.isPuhActOptions = response.data;
    });
    this.getDicts("sys_user_sex").then(response => {
      this.sexOptions = response.data;
    });
    this.getDicts("member_level").then(response => {
      this.levelIdOptions = response.data;
    });
  },
  methods: {
    //放大图片
    showImg(e){
      let array=[];
      array.push(this.imgpre+e);
      return array;
    },
    /** 重置密码按钮操作 */
    handleResetPwd(row) {
      this.$prompt('请输入"' + row.nickName + '"的新密码', "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消"
      }).then(({ value }) => {
        resetUserPwd(row.id, value).then(response => {
          if (response.code === 200) {
            this.msgSuccess("修改成功，新密码是：" + value);
          } else {
            this.msgError(response.msg);
          }
        });
      }).catch(() => {});
    },
    /** 查询会员基本信息列表 */
    getList() {
      this.loading = true;
      listMemberbasis(this.queryParams).then(response => {
        this.memberbasisList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },

    // 聊天是否推送字典翻译
    sexFormat(row, column) {
      return this.selectDictLabel(this.sexOptions, row.sex);
    },
    // 聊天是否推送字典翻译
    isChatSendFormat(row, column) {
      return this.selectDictLabel(this.isChatSendOptions, row.isChatSend);
    },
    // 活动是否推送字典翻译
    isActSendFormat(row, column) {
      return this.selectDictLabel(this.isActSendOptions, row.isActSend);
    },
    // 系统推送字典翻译
    isSysSendFormat(row, column) {
      return this.selectDictLabel(this.isSysSendOptions, row.isSysSend);
    },
    // 是否允许发布活动字典翻译
    isPuhActFormat(row, column) {
      return this.selectDictLabel(this.isPuhActOptions, row.isPuhAct);
    },
    // 会员等级字典翻译
    levelIdFormat(row, column) {
      return this.selectDictLabel(this.levelIdOptions, row.levelId);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        isChatSend: undefined,
        isActSend: undefined,
        updateTime: undefined,
        updateBy: undefined,
        createTime: undefined,
        createBy: undefined,
        bak3: undefined,
        bak2: undefined,
        bak1: undefined,
        isSysSend: undefined,
        isPuhAct: undefined,
        levelName: undefined,
        levelId: undefined,
        passWord: undefined,
        userName: undefined,
        openId: undefined,
        avterImg: undefined,
        email: undefined,
        address: undefined,
        career: undefined,
        phone: undefined,
        brith: undefined,
        nickName: undefined,
        sex: undefined,
        id: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加会员基本信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getMemberbasis(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改会员基本信息";
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            updateMemberbasis(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              } else {
                this.msgError(response.msg);
              }
            });
          } else {
            addMemberbasis(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              } else {
                this.msgError(response.msg);
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除会员基本信息编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delMemberbasis(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有会员基本信息数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportMemberbasis(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function() {});
    }
  }
};
</script>
